package com.tis.std;

import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import jpa.entitys.synchro.staging.SyncQueue;
import org.apache.log4j.Logger;
import com.tis.QueueCommon;
import promis.db.connection.dbHelper;
import apps.promis.out.common.SyncQueueAction;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 12, 2013 : 3:15:09 PM 
 * CreateBy=Chatree Siangklom
 * 
 * QueueServer
 * TODO
 */
public class QueueServer extends QueueCommon{	
	private static final Logger log = Logger.getLogger(QueueServer.class);
	private static final long MEGABYTE = 1024L * 1024L;

	public static long bytesToMegabytes(long bytes) {		  		  
		  return (bytes / MEGABYTE);
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 3:15:11 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	protected static void getQueue()throws Exception {
		try{
			dbHelper dbHelp = new dbHelper();
			Connection conn = dbHelp.getConn();
			ExecutorService executor = Executors.newFixedThreadPool(5);
			
			try {				
				 while(true)
				 {
					 SyncQueueAction queueAction = new SyncQueueAction();
					 QueueProcessing qServ = new QueueProcessing();
					 SyncQueue queue = queueAction.GetQueue();
					 if(queue != null){
						 
						 
						 Runnable worker = new WorkerThread("" + queue.getQueueId(), conn, queue);
				         executor.execute(worker);
						 
						 //conn = dbHelp.getConn();
						 //qServ.ProcRun(queue, conn);
					 	
					 }
					 	// Get the Java runtime
					    Runtime runtime = Runtime.getRuntime();
				    	// Calculate the used memory		    
					    long memory = runtime.totalMemory() - runtime.freeMemory();
					    System.out.println("totalMemory: " + bytesToMegabytes(runtime.totalMemory()) + "MB." 
		    							+ " //freeMemory: " + bytesToMegabytes(runtime.freeMemory()) + "MB." 
		    							+ " //Used memory is bytes: " +  memory
		    							+ " //Used memory is megabytes: " + bytesToMegabytes(memory)  + "MB.");
					    
					    // Run the garbage collector
					    runtime.gc();
					    //System.gc();
				    Thread.sleep(2000);
				    qServ = null;
				    queue = null;
				    queueAction = null;
				 }
			} catch (Exception e) {
				e.printStackTrace();
				
				//////////////////////////////////////////////////////////////
				executor.shutdown();					        
		        while (!executor.isTerminated()) {}
		        executor = null;
		        System.err.println("Finished of threads Exception =" + e.getMessage());
		        //////////////////////////////////////////////////////////////
		        
				log.error(e.getMessage());
				System.out.println(e.getMessage());
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.out.println(e.getMessage());
			getQueue();
		}
	}
	
	/**
	 * 
	 * Date =Nov 12, 2013 : 3:15:15 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			
			getQueue();
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.out.println(e.getMessage());
		}
	}	
}
